package com.bfly.mall.user.api;

import com.alibaba.fastjson.JSONObject;
import com.bfly.common.ResponseUtil;
import com.bfly.common.json.JsonUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.core.security.Login;
import com.bfly.mall.user.dto.UserListDTO;
import com.bfly.mall.user.entity.User;
import com.bfly.mall.user.enums.PlatformType;
import com.bfly.mall.user.service.IUserService;
import com.bfly.mall.wechat.minipro.service.WeChatMiniProAPi;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 会员Api Controller
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/18 14:40
 */
@RestController
@RequestMapping(value = "/api/user")
public class UserApiController extends BaseController {

    @Autowired
    private IUserService userService;

    /**
     * 获得当前会员信息
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/28 13:39
     */
    @GetMapping(value = "/current/info")
    @Login
    public void getCurrentInfo(HttpServletResponse response) {
        User user = userService.getUser(getUser().getOpenId());
        UserListDTO dto = new UserListDTO();
        BeanUtils.copyProperties(user, dto);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(dto));
    }

    /**
     * 解密微信用户手机号码
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/10 20:46
     */
    @GetMapping(value = "/phone/decrypt")
    @Login
    public void decryptPhoneNumber(HttpServletResponse response, HttpServletRequest request, @RequestParam("type") int type) {
        PlatformType platformType = PlatformType.getPlatformType(type);
        User user = getUser();
        switch (platformType) {
            case WECHAT_MINIPRO:
                String data = request.getParameter("data");
                String iv = request.getParameter("iv");
                if (StringUtils.isEmpty(data) || StringUtils.isEmpty(iv)) {
                    throw new ServiceResponseException(SysError.MISSING_PARAM, "缺少参数");
                }
                String sessionKey = request.getHeader("session-key");
                String phoneJson = WeChatMiniProAPi.decryptMiniProData(data, sessionKey, iv);
                JSONObject json = JsonUtil.parse(phoneJson, JSONObject.class);
                String phone = json.getString("purePhoneNumber");
                userService.bindPhone(user.getId(), phone);
                break;
            default:
                throw new ServiceResponseException(SysError.PARAM_ERROR, "位置的平台类型");
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}
